//
//  NSString+Addtions.swift
//  B2BAutoziMall
//
//  Created by heew on 16/1/31.
//  Copyright © 2016年 qeegoo. All rights reserved.
//

import UIKit

extension NSString {
    
//    func stringFromMD5() -> NSString {
//        var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
//        if let data = self.data(using: String.Encoding.utf8.rawValue) {
//            CC_MD5(data.bytes, CC_LONG(data.count), &digest)
//        }
//        let digestHex = digest.map { String(format: "%02x", $0) }.joined(separator: "")
//        return digestHex as NSString
//    }
    
    /// 快速返回一个缓存目录的路径
    func cacheDir() -> String
    {
        let cachePath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last!
        return (cachePath as NSString).appendingPathComponent(self.pathComponents.last!)
    }
    
    // 验证电话号码
    func isPhoneNumber()->Bool{
        let phone = "^((1[34578][0-9]{9})|((0\\d{2}-\\d{8})|(0\\d{3}-\\d{7,8})|(0\\d{10,11}))|(((400)-(\\d{3})-(\\d{4}))|((400)-(\\d{4})-(\\d{3}))|((400)-(\\d{5})-(\\d{2}))|(400\\d{7}))|(((800)-(\\d{3})-(\\d{4}))|((800)-(\\d{4})-(\\d{3}))|((800)-(\\d{5})-(\\d{2}))|(800\\d{7})))$"
        let refextePhone = NSPredicate(format: "SELF MATCHES %@", phone)
        if refextePhone.evaluate(with: self) == true{
            return true
        }
        return false
    }
    
    func isMobilePhone()->Bool {
        let mobilePhone = "^((1[34578][0-9]{9})|((0\\d{2}-\\d{8})|(0\\d{3}-\\d{7,8})|(0\\d{10,11}))$"
        let refexMobilePhone = NSPredicate(format: "SELF MATCHES %@", mobilePhone)
        if refexMobilePhone.evaluate(with: self) == true { return true }
        return false
    }
    
    /**
    *  车牌号验证
    */
    func isValidCarNo() -> Bool {
        let carRegex = "^[A-Za-z]{1}[A-Za-z_0-9]{5}$"
        let carTest = NSPredicate(format: "SELF MATCHES %@", carRegex)
        return carTest.evaluate(with: self)
    }
    
    /**
    *  邮政编码
     */
    func isValidPostalcode() -> Bool {
        let postalCodeRegex = "^[0-8]\\d{5}(?!\\d)$"
        let postalCodeTest = NSPredicate(format: "SELF MATCHES %@", postalCodeRegex)
        return postalCodeTest.evaluate(with: self)
    }
}

extension String {
//    mutating func hideCenterFourNumer() -> String{
////       let s = startIndex.advancedBy(3)
////       let s1 = self[3,
//        let s = startIndex.advancedBy(3)
//        let e = startIndex.advancedBy(7)
//        self.replaceSubrange(s..<e, with: "****")
//        return self
//    }
//    
//    func subString(startIndex: Int, length: Int) -> String {
//        let start = self.startIndex.advancedBy(startIndex)
//        let end = self.startIndex.advancedBy(startIndex + length)
//        return self.substring(with: start..<end)
//    }
//    
//    func trimlastCharaters(num:Int) -> String {
//        let start = self.startIndex
//        let end = self.endIndex.advancedBy(-num)
//        return self.substring(with: start..<end)
//    }
}
